import axios, { AxiosInstance, AxiosRequestConfig } from "axios"

// 泛型，ts装包
export interface ResponseData<T> {
  code: number
  data?: T
  msg?: string
}

class HttpRequest {
  public baseURL = import.meta.env.DEV ? "/api" : "/" // 基本路径，开发环境"/api"
  public timeout = 3000

  // 每次请求都创建一个独一无二的实例，为了保证 请求之间是互不干扰的
  public request(options: AxiosRequestConfig) {
    const instance = axios.create() // 创建一个axios实例
    options = this.mergeOptions(options)
    this.setInterceptors(instance)
    return instance(options) //最终发送请求的核心
  }
  setInterceptors(instance: AxiosInstance) {
    // 请求拦截
    instance.interceptors.request.use(
      (config) => {
        // config.headers!["Accept-Encoding"] = "gzip"
        // config.headers!["Content-Type"] = "application/json"

        config.headers!["token"] = "bearer token"
        return config
      },
      (err) => {
        return Promise.reject(err)
      }
    )
    // 响应拦截
    instance.interceptors.response.use(
      (res) => {
        const { code } = res.data
        if (code != 0) {
          return Promise.reject(res)
        }

        // 401 403 404 500 状态码处理
        return res
      },
      (err) => {
        return Promise.reject(err)
      }
    )
  }
  mergeOptions(options: AxiosRequestConfig) {
    return Object.assign(
      {
        baseURL: this.baseURL,
        timeout: this.timeout
      },
      options
    )
  }
  public get<T>(url: string, data?: unknown): Promise<ResponseData<T>> {
    // res.data.data
    return this.request({
      method: "get",
      url,
      params: data
    }).then(
      (res) => {
        return Promise.resolve(res.data)
      },
      (err) => {
        if (err.status == 200) return Promise.resolve(err.data)
        return Promise.reject(err)
      }
    )
  }
  public post<T>(url: string, params: any, headers: any): Promise<ResponseData<T>> {
    return fetch('/api/' + url, {
      method: 'POST',
      body: params,
      headers
    })

      /*  return this.request({
        method: "post",
        url,
        data: params
        , headers
      }) */
      .then(
        (res) => {
          return Promise.resolve(res.data)
        },
        (err) => {
          if (err.status == 200) return Promise.resolve(err.data)
          return Promise.reject(err)
        }
      )
  }
}

// 请求取消 需要维护页面的状态
export default new HttpRequest()

// login<{username;token}>("url",data)
